// src/services/mqttService.js  
import Paho from 'paho-mqtt';

//const client = new Paho.Client('ws://cy.kingvcn.com:20008/mqtt', 'clientId_' + Math.random().toString(16).substr(2, 8));
const client = new Paho.Client('wss://kv.cjhzcn.com:42010/mqtt', 'clientId_' + Math.random().toString(16).substr(2, 8));

function onsubscribe(topic) {
  client.subscribe(topic);
}

function sendMessage(topic, message) {
  if (client.isConnected()) {
    client.send(topic, message);
    client.onConnectionLost
  } else {
    console.log('MQTT client is not connected');
  }
}

// function onMessageArrived(message) {
//   console.log('Message arrived:', message.payloadString, message);
//   // 你可以在这里处理接收到的消息，例如将其存储在 Vuex 或 Vue 3 的响应式状态中  
// }

function onMessage(getMessage) {
  client.onMessageArrived = getMessage;
}

function onconnect(options) {
  client.connect(options);
}
function onDisconnect() {
  client.disconnect()
}


export default {
  client,
  sendMessage,
  onconnect,
  onMessage,
  onDisconnect,
  onsubscribe,
};